<template>
  <div class="app-container">
    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-form ref="form" inline :model="form" :rules="rules" label-width="80px">
      <el-form-item label="授权码" prop="code">
        <el-input
          style="width: 400px"
          type="textarea"
          :rows="1"
          v-model="form.code"
          placeholder="请输入授权码"/>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {listAuthCode, addAuthCode, updateAuthCode} from "@/api/mes/auth_code";

export default {
  name: "Code",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 是否显示弹出层
      open: true,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          {required: true, message: "授权码不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listAuthCode(this.queryParams).then(response => {
        this.form = response.rows[0] || {}
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAuthCode(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAuthCode(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
